package com.xiaoxin.lease.web.admin.controller.apartment;


import com.xiaoxin.lease.common.result.Result;
import com.xiaoxin.lease.model.entity.AttrKey;
import com.xiaoxin.lease.model.entity.AttrValue;
import com.xiaoxin.lease.web.admin.service.AttrKeyService;
import com.xiaoxin.lease.web.admin.service.AttrValueService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Tag(name = "房间属性管理")
@RestController
@RequestMapping("/admin/attr")
public class AttrController {
    
    @Autowired
    private AttrKeyService attrKeyService;
    
    
    @Autowired
    private AttrValueService attrValueService;

    @Operation(summary = "新增或更新属性名称")
    @PostMapping("key/saveOrUpdate")
    public Result saveOrUpdateAttrKey(@RequestBody AttrKey attrKey) {
        return Result.ok(attrKeyService.saveOrUpdate(attrKey));
    }

    @Operation(summary = "新增或更新属性值")
    @PostMapping("value/saveOrUpdate")
    public Result saveOrUpdateAttrValue(@RequestBody AttrValue attrValue) {

        return Result.ok(attrValueService.saveOrUpdate(attrValue));
    }


    @Operation(summary = "查询全部属性名称和属性值列表")
    @GetMapping("list")
    public Result listAttrInfo() {

        //实现方式1
        /** List<AttrKeyVo> attrKeyVoList = new ArrayList<>();
       List<AttrKey> attrKeyList = attrKeyService.list();
       attrKeyList.forEach(attrKey -> {
           List<AttrValue> attrValueList = attrValueService.list(new LambdaQueryWrapper<AttrValue>().eq(AttrValue::getAttrKeyId,attrKey.getId()));
           AttrKeyVo attrKeyVo = new AttrKeyVo();
           BeanUtils.copyProperties(attrKey, attrKeyVo);

           // attrKeyVo.setId(attrKey.getId());
         //  attrKeyVo.setName(attrKey.getName());
           attrKeyVo.setAttrValueList(attrValueList);
           attrKeyVoList.add(attrKeyVo);
       });*/
        //实现方式2
        return Result.ok(attrKeyService.getAttrKeylist());
    }

    @Operation(summary = "根据id删除属性名称")
    @DeleteMapping("key/deleteById")
    public Result removeAttrKeyById(@RequestParam Long attrKeyId) {

        return Result.ok(attrKeyService.removeById(attrKeyId));
    }

    @Operation(summary = "根据id删除属性值")
    @DeleteMapping("value/deleteById")
    public Result removeAttrValueById(@RequestParam Long id) {

        return Result.ok(attrValueService.removeById(id));
    }

}
